/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import control.MStockMidlet;
import font.MyFont;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import listener.Command;
import listener.ICommandListener;
import listener.IKeyListener;
import view.StyleManager;

/**
 *
 * @author kien.tran
 */
public abstract class ACanvas extends Canvas {

    protected final int MOVE_NONE = 0;
    protected final int MOVE_UP = 1;
    protected final int MOVE_DOWN = 2;
    protected final int TITLE_LEFT = 0;
    protected final int TITLE_CENTER = 1;
    protected final int TITLE_RIGHT = 2;
    protected Display display;
    protected String[] captionRows;
    protected String caption;
    protected Image background;
    protected Image icon;
    public int KEY_SOFT_LEFT;
    public int KEY_SOFT_RIGHT;
    private Style headerStyle, titleStyle, iconStyle, footerStyle, bodyStyle;
    private StyleManager style;
    private MyFont font;
    private int itemX, itemY, itemWidth, itemHeight, width, height, headerPaddingTop = 0;
    private Command selectCommand, cancelCommand;
    private ICommandListener commandListener;
    private IKeyListener keyListener;
    protected int oldIndex = 0, newIndex = 0, firstIndex = -1, lastIndex = -1, moveAction = MOVE_NONE;
    private boolean autoRepaint = true;
    protected int titleAlign = 0;

    public ACanvas() {
    }

    public ACanvas(Display display, MStockMidlet control, String caption, Image background, Image icon) {
        setFullScreenMode(true);
        this.display = display;
        this.background = background;
        this.width = getWidth();
        this.height = getHeight();
        this.font = control.getFont();
        this.headerStyle = control.getStyle().getHeaderStyle();
        this.iconStyle = control.getStyle().getIconStyle();
        this.titleStyle = control.getStyle().getTitleStyle();
        this.footerStyle = control.getStyle().getFooterStyle();
        this.bodyStyle = control.getStyle().getBodyStyle();
        this.style = control.getStyle();
        this.icon = icon;
        this.caption = caption;
        init();
    }

    public Image getBackground() {
        return background;
    }

    public void setBackground(Image background) {
        this.background = background;
    }

    public Display getDisplay() {
        return display;
    }

    public void setDisplay(Display display) {
        this.display = display;
    }

    public Image getIcon() {
        return icon;
    }

    public void setIcon(Image icon) {
        this.icon = icon;
    }

    public ICommandListener getCommandListener() {
        return commandListener;
    }

    public void setCommandListener(ICommandListener commandListener) {
        this.commandListener = commandListener;
    }

    public IKeyListener getKeyListener() {
        return keyListener;
    }

    public void setKeyListener(IKeyListener keyListener) {
        this.keyListener = keyListener;
    }

    public Command getCancelCommand() {
        return cancelCommand;
    }

    public void setCancelCommand(Command cancelCommand) {
        this.cancelCommand = cancelCommand;
    }

    public Command getSelectCommand() {
        return selectCommand;
    }

    public void setSelectCommand(Command selectCommand) {
        this.selectCommand = selectCommand;
    }

    public StyleManager getStyle() {
        return style;
    }

    public void setStyle(StyleManager style) {
        this.style = style;
    }

    public int getItemHeight() {
        return itemHeight;
    }

    public void setItemHeight(int itemHeight) {
        this.itemHeight = itemHeight;
    }

    public int getItemWidth() {
        return itemWidth;
    }

    public void setItemWidth(int itemWidth) {
        this.itemWidth = itemWidth;
    }

    public int getItemX() {
        return itemX;
    }

    public void setItemX(int itemX) {
        this.itemX = itemX;
    }

    public int getItemY() {
        return itemY;
    }

    public void setItemY(int itemY) {
        this.itemY = itemY;
    }

    public MyFont getFont() {
        return font;
    }

    public void setFont(MyFont font) {
        this.font = font;
    }

    public boolean isAutoRepaint() {
        return autoRepaint;
    }

    public void setAutoRepaint(boolean autoRepaint) {
        this.autoRepaint = autoRepaint;
    }

    public void show() {
        this.display.setCurrent(this);
    }

    protected void paint(Graphics g) {
        setFullScreenMode(true);
        g.setColor(bodyStyle.getBgColor());
        g.fillRect(0, 0, width, height);
        this.width = getWidth();
        this.height = getHeight();
        init();
        if (background != null) {
            int x = itemX + (itemWidth - background.getWidth()) / 2;
            int y = itemY + (itemHeight - background.getHeight()) / 2;
            g.drawRegion(background, 0, 0, background.getWidth(), background.getHeight(), Sprite.TRANS_NONE, x, y, Graphics.TOP | Graphics.LEFT);
        }

        // paint
        paintItems(g);
        paintHeader(g);
        paintFooter(g);
    }

    public void paintHeader(Graphics g) {
        int tempX, tempY;
        g.setColor(headerStyle.getBgColor());
        g.fillRect(0, 0, getWidth(), headerStyle.getHeight());

        // paint icon
        tempX = headerStyle.getPadding().getLeft() + iconStyle.getPadding().getLeft();
        tempY = headerStyle.getPadding().getTop() + iconStyle.getPadding().getTop();
        if (icon != null) {
            g.drawImage(icon, tempX, tempY, Graphics.TOP | Graphics.LEFT);
        }

        // paint text
        if (titleAlign == TITLE_LEFT) {
            tempX = headerStyle.getPadding().getLeft() + iconStyle.getWidth() + titleStyle.getPadding().getLeft();
            tempY = headerStyle.getPadding().getTop() + titleStyle.getPadding().getTop() + headerPaddingTop;
            for (int i = 0; i < captionRows.length; i++) {
//                boldFont.drawString(captionRows[i], tempX, tempY, Font2.LEFT, g);

                // TEST
                g.setColor(0xC6637A);

                font.getFontBold().drawString(g, captionRows[i], tempX, tempY, Graphics.TOP | Graphics.LEFT);
//                tempY += boldFont.getHeight() + titleStyle.getRowHeight();
                tempY += font.getFontBold().getHeight() + titleStyle.getRowHeight();
            }
        } else if (titleAlign == TITLE_CENTER) {
            int t = getWidth() - headerStyle.getPadding().getRight() - titleStyle.getPadding().getRight();
            tempX = headerStyle.getPadding().getLeft() + iconStyle.getWidth() + titleStyle.getPadding().getLeft();
            tempX = (t - tempX) / 2 + tempX;
            tempY = headerStyle.getPadding().getTop() + titleStyle.getPadding().getTop() + headerPaddingTop;
            for (int i = 0; i < captionRows.length; i++) {
//                boldFont.drawString(captionRows[i], tempX, tempY, Font2.CENTER, g);
                // TEST
                g.setColor(0xC6637A);

                font.getFontBold().drawString(g, captionRows[i], tempX, tempY, Graphics.TOP | Graphics.HCENTER);
//                tempY += boldFont.getHeight() + titleStyle.getRowHeight();
                tempY += font.getFontBold().getHeight() + titleStyle.getRowHeight();
            }
        } else if (titleAlign == TITLE_RIGHT) {
            tempX = headerStyle.getPadding().getRight() + titleStyle.getPadding().getRight();
            tempY = headerStyle.getPadding().getTop() + titleStyle.getPadding().getTop() + headerPaddingTop;
            for (int i = 0; i < captionRows.length; i++) {
//                boldFont.drawString(captionRows[i], tempX, tempY, Font2.RIGHT, g);
                // TEST
                g.setColor(0xC6637A);

                font.getFontBold().drawString(g, captionRows[i], tempX, tempY, Graphics.TOP | Graphics.RIGHT);
//                tempY += boldFont.getHeight() + titleStyle.getRowHeight();
                tempY += font.getFontBold().getHeight() + titleStyle.getRowHeight();
            }
        }
    }

    public void paintFooter(Graphics g) {
        g.setColor(footerStyle.getBgColor());
        g.fillRect(0, getHeight() - footerStyle.getHeight(), getWidth(), footerStyle.getHeight());

        // paint left command
        if (selectCommand != null) {
//            boldFont.drawString(selectCommand.getText(), footerStyle.getPadding().getLeft(), getHeight() - footerStyle.getHeight() + footerStyle.getPadding().getTop(), Font2.LEFT, g);
            // TEST
            g.setColor(0xC6637A);

            font.getFontBold().drawString(g, selectCommand.getText(), footerStyle.getPadding().getLeft(), getHeight() - footerStyle.getHeight() + footerStyle.getPadding().getTop(), Graphics.TOP | Graphics.LEFT);
        }

        // paint right command
        if (cancelCommand != null) {
//            boldFont.drawString(cancelCommand.getText(), getWidth() - footerStyle.getPadding().getRight(), getHeight() - footerStyle.getHeight() + footerStyle.getPadding().getTop(), Font2.RIGHT, g);
            // TEST
            g.setColor(0xC6637A);

            font.getFontBold().drawString(g, cancelCommand.getText(), getWidth() - footerStyle.getPadding().getRight(), getHeight() - footerStyle.getHeight() + footerStyle.getPadding().getTop(), Graphics.TOP | Graphics.RIGHT);
        }
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
        init();
    }

    public abstract void paintItems(Graphics g);

    public int getHeaderHeight() {
        return headerStyle.getHeight();
    }

    public int getFooterHeight() {
        return footerStyle.getHeight();
    }

    private void init() {
        initHeader(icon, caption);
        initFooter();
        this.itemHeight = getHeight() - headerStyle.getHeight() - footerStyle.getHeight();
    }

    private void initHeader(Image icon, String caption) {
        int headerH = 0;

        if (icon != null) {
            this.icon = icon;
            iconStyle.setHeight(icon.getHeight() + iconStyle.getPadding().getTopAndBottom());
            iconStyle.setWidth(icon.getWidth() + iconStyle.getPadding().getLeftAndRight());
        } else {
            iconStyle.setHeight(0);
            iconStyle.setWidth(0);
        }

        if (caption.length() > 0) {
            titleStyle.setWidth(width - iconStyle.getWidth() - headerStyle.getPadding().getLeftAndRight());
//            this.captionRows = boldFont.splitWith(caption, titleStyle.getWidth() - titleStyle.getPadding().getLeftAndRight());
            // TEST
            this.captionRows = new String[]{caption};
            
            titleStyle.setHeight(captionRows.length * (font.getFontBold().getHeight() + titleStyle.getPadding().getTop()) + titleStyle.getPadding().getBottom());
        } else {
            titleStyle.setHeight(0);
            titleStyle.setWidth(0);
        }

        if (iconStyle.getHeight() > titleStyle.getHeight()) {
            this.headerPaddingTop = (iconStyle.getHeight() - titleStyle.getHeight()) / 2;
            headerH = iconStyle.getHeight();
        } else {
            this.headerPaddingTop = 0;
            headerH = titleStyle.getHeight();
        }

        if (headerH > 0) {
            headerH += headerStyle.getPadding().getTopAndBottom();
        }

        this.headerStyle.setHeight(headerH);
        this.itemY = headerH + bodyStyle.getPadding().getTop();
        this.itemX = bodyStyle.getPadding().getLeft();
        this.itemWidth = getWidth() - bodyStyle.getPadding().getLeftAndRight();
    }

    private void initFooter() {
        if (selectCommand != null || cancelCommand != null) {
            footerStyle.setHeight(font.getFontBold().getHeight() + footerStyle.getPadding().getTopAndBottom());
        } else {
            footerStyle.setHeight(0);
        }
    }

//    protected int getLeftSoftKey() {
//        return SoftKey.getLeftSoftkeyCode(this);
//    }
//
//    protected int getRightSoftKey() {
//        return SoftKey.getRightSoftkeyCode(this);
//    }
    protected int getLeftSoftKey() {
        return -6;
    }

    protected int getRightSoftKey() {
        return -7;
    }
}
